package com.jxd.project.wzgl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxd.project.wzgl.dao.IApplicationDao;
import com.jxd.project.wzgl.dto.ApplicationDTO;
import com.jxd.project.wzgl.dto.QueryApplicationDTO;
import com.jxd.project.menu.model.Application;
import com.jxd.project.menu.model.ApplicationGoods;
import com.jxd.project.menu.model.Log;
import com.jxd.project.wzgl.service.IApplicationGoodsService;
import com.jxd.project.wzgl.service.IApplicationService;
import com.jxd.project.wzgl.service.IGoodsService;
import com.jxd.project.wzgl.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ApplicationServiceImpl
 * @Description TODO
 * @Author 刘成
 * @Date 2025/10/1 19:30
 * @Version 1.0
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<IApplicationDao, Application> implements IApplicationService {
    @Autowired
    @Qualifier("applicationServiceImpl")
    private IApplicationService applicationService;
    @Autowired
    private IApplicationDao applicationDao;
    @Autowired
    @Qualifier("applicationGoodsServiceImpl")
    private IApplicationGoodsService applicationGoodsService;
    @Autowired
    @Qualifier("logServiceImpl")
    private ILogService logService;
    @Autowired
    @Qualifier("goodsServiceImpl")
    private IGoodsService goodsService;

    @Override
    @Transactional
    public Map<String,Object> addApplication(ApplicationDTO applicationDTO) {
        Map<String,Object> map = new HashMap<>();
        Application application = applicationDTO.getApplication();

        boolean flag2 = true;
        if (application.getApplicationId() != 0) {
            flag2 = applicationGoodsService.delWithApplicationId(application.getApplicationId());
        }

        //未知错误导致不能正常更新，后续完善
        boolean flag1 = true;
        if (application.getApplicationId() == 0) {
            flag1 = applicationService.saveOrUpdate(application);
        } else {
            flag1 = applicationDao.updateById(application) > 0;
        }

        applicationDTO.getApplicationGoods().forEach(applicationGoods -> {
            applicationGoods.setApplicationId(application.getApplicationId());
            applicationGoods.setBrid(0);
        });
        boolean flag3 = applicationGoodsService.saveOrUpdateBatch(applicationDTO.getApplicationGoods());
        if (application.getCheckType() == 2) {
            Log log = new Log();
            log.setLogTime(application.getApplicationTime());
            log.setLogDetail("提交");
            log.setLogState(1);
            log.setStartId(application.getApplicationEmpNo());
            log.setLogId(application.getApplicationId());
            if ("借用申请".equals(application.getApplicationName())) {
                log.setLogType(1);//代表借用申请
            } else if ("归还申请".equals(application.getApplicationName())) {
                log.setLogType(2);//代表归还申请
            }
            boolean flag4 = logService.saveOrUpdate(log);
            map.put("statu", flag1 && flag2 && flag3 && flag4);
            map.put("applicationId", application.getApplicationId());
            return map;
        }
        map.put("statu", flag1 && flag2 && flag3);
        map.put("applicationId", application.getApplicationId());
        return map;
    }

    @Override
    @Transactional
    public boolean updateApplication(ApplicationDTO application) {
        boolean flag1 = applicationService.saveOrUpdate(application.getApplication());
        boolean flag2 = applicationGoodsService.delWithApplicationId(application.getApplication().getApplicationId());
        boolean flag3 = applicationGoodsService.saveOrUpdateBatch(application.getApplicationGoods());
//        boolean flag4 = logService.delWithApplicationId(application.getApplication().getApplicationId());
        boolean flag5 = logService.saveOrUpdate(application.getLog());
        return flag1 && flag2 && flag3 && flag5;
    }

    @Override
    @Transactional
    public boolean updateApplicationStatus(ApplicationDTO applicationDTO) {
        Application application = applicationDTO.getApplication();

        boolean flag2 = true;
        List<Integer> brIds = new ArrayList<>();
        if (application.getCheckType() == 4){
            for (ApplicationGoods applicationGoods : applicationDTO.getApplicationGoods()) {
                if("借用申请".equals(application.getApplicationName())) {
                    flag2 = goodsService.updateGoodsNum(applicationGoods.getGoodsId(), -applicationGoods.getBrNum());
                } else if("归还申请".equals(application.getApplicationName())) {
                    flag2 = goodsService.updateGoodsNum(applicationGoods.getGoodsId(), applicationGoods.getBrNum());
                    brIds.add(applicationGoods.getIsReturn());
                }
            }
        }
        //冲销已借用的物资
        if (!brIds.isEmpty()){
            //如果归还物资信息已经在其他申请中，则将其他的申请状态改为保存
            boolean result2 = applicationGoodsService.resetApplicationStatus(brIds);
            //冲销已借的物资
            boolean result1 = applicationGoodsService.returnGoods(brIds);
        }
        boolean flag1 = applicationDao.updateById(application) > 0;

        Log log = new Log();
        log.setLogTime(application.getApplicationTime());
        log.setLogDetail("审核");
        if (application.getBannedReason() == null) {
            log.setLogState(1);
        } else {
            log.setLogState(0);
            log.setLogDetail(application.getBannedReason());
        }
        log.setStartId(application.getApplicationEmpNo());
        log.setEndId(application.getAuditEmpNo());
        log.setLogId(application.getApplicationId());
        if ("借用申请".equals(application.getApplicationName())) {
            log.setLogType(1);//代表借用申请
        } else if ("归还申请".equals(application.getApplicationName())) {
            log.setLogType(2);//代表归还申请
        }
        boolean flag3 = logService.save(log);
        return flag1 && flag2 && flag3;
    }

    @Override
    @Transactional
    public boolean deleteApplication(List<Integer> applicationIDs) {
        boolean flag1 = applicationService.removeByIds(applicationIDs);
        boolean flag2 = applicationGoodsService.delWithApplicationIds(applicationIDs);
//        boolean flag3 = logService.delWithApplicationIds(applicationIDs);逻辑冲突，保存申请单时没有添加过日志，删除时会出错
        return flag1 && flag2;
    }

    @Override
    public Map<String,Object> getApplicationWithQueryApplicationDTO(QueryApplicationDTO queryApplicationDTO){
        Page<Application> page = new Page<>(queryApplicationDTO.getCurrent(),queryApplicationDTO.getSize());
        List<Application> list = applicationDao.selectApplicationWithDTO(page, queryApplicationDTO);
        int total = (int)page.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("total",total);
        map.put("data",list);
        return map;
    }

    @Override
    public int getDoubleIdentityBorrowBackLogCount(int empNo){
        return applicationDao.selectDoubleIdentityBorrowBackLogCount(empNo);
    }
}
